package com.example.minzhengtong.base

import android.app.Activity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.annotation.Nullable
import com.example.minzhengtong.R
import com.example.minzhengtong.utils.UIUtils
import com.gyf.immersionbar.ImmersionBar
import com.gyf.immersionbar.components.ImmersionFragment
import com.kaopiz.kprogresshud.KProgressHUD
import java.util.*

abstract class BaseFg : ImmersionFragment()/*,
    View.OnClickListener*/ {
    companion object {


    }

    override fun immersionBarEnabled(): Boolean {
        return true
    }

    /**
     * 视图是否加载完毕
     */
    private var isViewPrepare = false

    /**
     * 数据是否加载过了
     */
    private var hasLoadData = false

    /**
     * 初始化 View
     */
    abstract fun initView()

    /**
     * 初始化 View点击监听
     */
    abstract fun initListener()

    /**
     * 懒加载
     */
    abstract fun lazyLoad()

    /**
     * 是否使用 EventBus
     */
    open fun useEventBus(): Boolean = true

    /**
     * 开始请求
     */
    open fun ReGetData() {}

    /**
     * 无网状态—>有网状态 的自动重连操作，子类可重写该方法
     */
    open fun doReConnected() {
        ReGetData()
    }

    open fun showToast(msg: String?){
        UIUtils.showCenterToast(msg)
    }

    /**
     * 上下文context
     */
    var baseContext: Activity = Activity()
    var mImmersionBar: ImmersionBar? = null

    /**
     * 分页加载页数
     */
    var pageNum = 1

    /**
     *  网络是否可用
     *
     */
    var ISNetWork = false

    private var imm: InputMethodManager? = null


    override fun initImmersionBar() {
    }

    override fun onViewCreated(view: View, @Nullable savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        baseContext = activity!!
        isViewPrepare = true
        loadingDialog = KProgressHUD.create(this.baseContext).setStyle(KProgressHUD.Style.SPIN_INDETERMINATE).setCancellable(
            false
        ).setAnimationSpeed(2).setDimAmount(0.5f)

        ImmersionBar.with(this).navigationBarColor(R.color.white).init()

        initView()
        initListener()
        lazyLoadDataIfPrepared()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden){
            ImmersionBar.with(this).navigationBarColor(R.color.white).init()
        }
    }

    open fun requestSuccess(json : String){

    }


    open fun getData(isLoad: Boolean = false) {

    }

    /*override fun onClick(v: View?) {
    }*/

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            lazyLoadDataIfPrepared()
        }
    }

    private fun lazyLoadDataIfPrepared() {
        if (userVisibleHint && isViewPrepare && !hasLoadData) {
            lazyLoad()
            hasLoadData = true
        }
    }

    protected var loadingDialog: KProgressHUD? = null

    /**
     * 加载中
     */
    fun show_Loading() {
        loadingDialog?.show()
    }

    /**
     * 取消加载
     */
    fun hide_Loading() {
        loadingDialog?.dismiss()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

}
